package de.lindenvalley.ergo;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import util.AnotherFont;

import de.lindenvalley.ergo.drag.DragActivity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class ResultActivity extends Activity implements OnClickListener {
	
	private String fileString;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.result);
        
        AnotherFont.findTypeface((ViewGroup)getWindow().getDecorView());
        
        fileString = getIntent().getStringExtra("file");
        if (fileString != null){
        	ImageView iv = (ImageView) findViewById(R.id.imageLine);
        	FileInputStream fin;
			try {
				fin = new FileInputStream(fileString);
				BitmapDrawable drawable = new BitmapDrawable(fin);
	        	iv.setBackgroundDrawable(drawable);
	        	fin.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
        }
        View buttonApply = findViewById(R.id.buttonApply);
        View buttonRetry = findViewById(R.id.buttonRetry);
        View logo = findViewById(R.id.imageViewLogo);
        
        buttonApply.setOnClickListener(this);
        buttonRetry.setOnClickListener(this);
        logo.setOnClickListener(this);
        
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		Intent intent = null;
		boolean needFinish = true;
		switch (id) {
		case R.id.buttonApply:
			intent = new Intent(this, DragActivity.class);
			intent.putExtra("file", fileString);
			break;

		case R.id.buttonRetry:
			intent = new Intent(this, CameraActivity.class);
			break;
			
		case R.id.imageViewLogo:
			intent = new Intent (Intent.ACTION_VIEW, Uri.parse(getString(R.string.urlLink)));
			needFinish = false;
			break;
		}
		
		if (intent != null){
			startActivity(intent);
			if (needFinish)
				finish();
		}
	}

}
